/**
 * Created by hcadmin on 16/10/18.
 */
$(function(){
    var search;
    var key;
    var index;
    //进入页面展示的数据

    var localstorage = window.localStorage;
    var keyword = localstorage.getItem("keyword");
    if (keyword != null && keyword.length > 0){

        search = "1";
        $(".search").val(keyword);
        $(".search").addClass('searching');
        //搜索
        initSearch(keyword);
    }else {
        //加载默认页面
        load();
    }




    //var stop=true;//触发开关，防止多次调用事件
    //$(window).scroll(function() {
    //    //当内容滚动到底部时加载新的内容 100当距离最底部100个像素时开始加载.
    //    if ($(this).scrollTop() + $(document).height() +100>= $(document).height()) {
    //
    //        if (stop == true) {
    //            stop = false;
    //            load();
    //            if(search = "1"){
    //                initSearch(key);
    //            }
    //        }
    //    }
    //});
    function load(){
        //var timestamp = $(".tableview_cell1").last().attr("time")==undefined?0:$(".tableview_cell1").last().attr("time");
        initData("bigest_content",localStorage.user_id,"2","2",0);
    }

   // 点击行跳转
   $(document).on('click','.right_small_up',function(){
       var approval_id  = $(this).attr("id");

       var status       = $(this).attr("status");
       if(status==0){
           index="2";
       }else{
           index="4";
       }
       if ($(".search").val().length > 0){
           localstorage.setItem("keyword",$(".search").val());
       }
       window.location.href="approvalDetail.html?index="+index+"&id="+approval_id+"";
   });

    //点击PING进行
    $(document).on('click','.little_icon',function(){
        //var _this = $(this).children(0);
        var id            = $(this).attr("id");      //获取
        var title         = $(this).attr("name");
        var r_id          = $(this).attr("id2");
        var name          = $(this).attr("userName");
        var loginName     = $(this).attr("loginName");
        var email         = $(this).attr("email");
        var phone         = $(this).attr("phone");
        var qyxBindId     = $(this).attr("qyxBindId");

        var data;
        var data_string;
        var formdata=new FormData();
        var hypertext = {
            attId:"",
            attName:"",
            attSize:"0",
            content:"审批链接",
            title:title
        };
        var receivers = [{
            "id":r_id,
            "name":name,
            "loginName":loginName,
            "email":email,
            "phone":phone,
            "qyxBindId":qyxBindId
        }];

        var data2 = {
            "userId":localStorage.user_id,
            "token":localStorage.token,
            "data":JSON.stringify({
                sendDate:getNowFormatDate(),
                deadline:"",
                delayDate:"",
                alertMode:"1",
                content:"审批",
                hyperlink:"/approval/html/approvalDetail.html?id="+id+"",
                hypertext:JSON.stringify(hypertext),
                imtoken:localStorage.imToken,
                qyxBindId:localStorage.qyxBindId,
                receivers:receivers,
                code:"5",
                sourceId:id
            })
            //"fileParams":[{"id":"111","type":"1"}]
        };

        $.ajax({
            async: true,
            crossDomain: true,
            //url: "http://202.91.248.189:5859/moa-server/app/ping/send",
            url:tk_apiurl+"ping/send",
            method: "POST",
            data:data2,
            jsonpCallback:"jsoncallback",
            dataType: "jsonp",
            success:function(d){
                var data = eval('('+d+')');
                var status = data.statusCode;
                var msg    = data.message;
                if(status==200){
                    console.log(data);
                    $(".call_back_success").html("提醒成功").show().fadeOut(2000);
                }else{
                    $(".call_back_success").html("提醒失败").show().fadeOut(2000);
                }
            }
        })
    });


    //点击  搜索
    $("#search").bind("input propertychange",function(){
        $(this).addClass('searching');
        key = $(this).val();
        $(".tableview_cell1").remove();
        search = "1";
        initSearch(key);
        if(key==""){
            $(this).removeClass('searching');
            localstorage.removeItem("keyword");
        }
    });
    function initSearch(keyword){
        //var timestamp = $(".tableview_cell1").last().attr("time")==undefined?0:$(".tableview_cell1").last().attr("time");
        initData("bigest_content",localStorage.user_id,"2","2",0,"1",keyword);
    }

    //获取当前时间
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
        return currentdate;
    }
});